/*
 * Copyright (c) 2008 Ricardo Rocha, rocha.porto@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.gliding.kumulus.client;

import java.util.Date;

import org.gliding.kumulus.client.model.PlanningContext;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class defining the menu of the Planning component.
 * 
 * @author Ricardo Rocha <rocha.porto@gmail.com>
 * @version $Rev:: $
 * 
 * @see org.gliding.kumulus.client.Planning
 */
public class PlanningMenu extends Composite 
	implements ClickListener, CalendarEventListener {

	/**
	 * Holds the different widgets that are part of the planning menu.
	 */
    private VerticalPanel menuPanel = new VerticalPanel();
    
    /**
     * A label show the currently selected date period.
     */
    private Label dateViewText;
    
    /**
     * The calendar widget for selected the date period.
     */
    private Calendar calendar;
    
    /**
     * The button to move the view panel to the next period.
     */
    private Image nextLink;
    
    /**
     * The button to move the view panel to the previous period.
     */
    private Image previousLink;
    
    /**
     * The button to set the selected period to the current date.
     */
    private Button todayButton;
    
    /**
     * The link to select the size of the view period to a week.
     */
    private Hyperlink weekLink;
    
    /**
     * The link to select the size of the view period to a month.
     */
    private Hyperlink monthLink;
    
    /**
     * The link to selected the size of the view period to 4 days.
     */
    private Hyperlink next4DaysLink;
    
    /**
     * The link to set french (FR) as the current interface language.
     */
    private Image frLink;
    
    /**
     * The link to set english (GB) as the current interface language.
     */
    private Image gbLink;
    
    /**
     * The link to set portuguese (PT) as the current interface language.
     */
    private Image ptLink;
    
	/**
	 * Class constructor.
	 * 
	 * Initializes the planning menu, adding all the necessary widgets.
	 */
	public PlanningMenu() {
		
		// We need the object containing the internationalized messages
		PlanningConstants planningConstants = (PlanningConstants) GWT.create(PlanningConstants.class);
        
        // Add the first layer of the menu (previousPeriod, nextPeriod, today)
        nextLink = new Image("imgs/next.jpg");
        nextLink.addClickListener(this);
        nextLink.setStyleName("link");
        previousLink = new Image("imgs/previous.jpg");
        previousLink.addClickListener(this);
        previousLink.setStyleName("link");
        todayButton = new Button(planningConstants.today());
        todayButton.addClickListener(this);

        HorizontalPanel prevNextLinksPanel = new HorizontalPanel();
        prevNextLinksPanel.setWidth("100%");
        prevNextLinksPanel.add(previousLink);
        prevNextLinksPanel.add(nextLink);
        prevNextLinksPanel.add(todayButton);
        menuPanel.add(new PlanningMenuWidget(prevNextLinksPanel, false, false));

        // Add the label panel showing the selected period
        VerticalPanel dateViewPanel = new VerticalPanel();
        dateViewPanel.setStyleName("kumulus-PlanningMenuHeader");
        dateViewText = new Label();
        dateViewPanel.add(dateViewText);
        menuPanel.add(new PlanningMenuWidget(dateViewPanel, false, false));
        
        // Add the calendar widget
        calendar = new Calendar();
        calendar.addCalendarUpdateListener(PlanningContext.getPlanningContext());
        menuPanel.add(new PlanningMenuWidget(calendar, true, true));

        // Add the different period size selection links (4days, week, month)
        SimplePanel next4DaysLinkPanel = new SimplePanel();
        next4DaysLinkPanel.setStyleName("kumulus-PlanningMenuLink");
        next4DaysLink = new Hyperlink(planningConstants.next4DaysView(), "next4");
        next4DaysLink.setTitle(planningConstants.next4DaysViewDesc());
        next4DaysLink.addClickListener(this);
        next4DaysLinkPanel.add(next4DaysLink);
        
        SimplePanel weekLinkPanel = new SimplePanel();
        weekLinkPanel.setStyleName("kumulus-PlanningMenuLink");
        weekLink = new Hyperlink(planningConstants.weekView(), "week");
        weekLink.setTitle(planningConstants.weekViewDesc());
        weekLink.addClickListener(this);
        weekLinkPanel.add(weekLink);
        
        SimplePanel monthLinkPanel = new SimplePanel();
        monthLinkPanel.setStyleName("kumulus-PlanningMenuLink");
        monthLink = new Hyperlink(planningConstants.monthView(), "month");
        monthLink.setTitle(planningConstants.monthViewDesc());
        monthLink.addClickListener(this);
        monthLinkPanel.add(monthLink);
        
        menuPanel.add(new PlanningMenuWidget(next4DaysLinkPanel, true, false));
        menuPanel.add(new PlanningMenuWidget(weekLinkPanel, true, false));
        menuPanel.add(new PlanningMenuWidget(monthLinkPanel, true, false));

        // Add the image links for setting the current internationalization
        HorizontalPanel i18nPanel = new HorizontalPanel();
        i18nPanel.setStyleName("kumulus-PlanningMenuI18N");        
        frLink = new Image("imgs/fr.png");
        frLink.addClickListener(this);
        gbLink = new Image("imgs/gb.png");
        gbLink.addClickListener(this);
        ptLink = new Image("imgs/pt.png");
        ptLink.addClickListener(this);
        i18nPanel.add(gbLink);
        i18nPanel.add(frLink);
        i18nPanel.add(ptLink);
        menuPanel.add(i18nPanel);
        
        // Add the tags with the color matching for the different roles
        VerticalPanel roleTagsPanel = new VerticalPanel();
        roleTagsPanel.setStyleName("kumulus-PlanningMenuRoleTags");
        SimplePanel instructorPanel = new SimplePanel();
        instructorPanel.setStyleName("kumulus-PlanningViewInstructor");
        instructorPanel.add(new Label(planningConstants.instructor()));
        roleTagsPanel.add(instructorPanel);
        SimplePanel studentPanel = new SimplePanel();
        studentPanel.setStyleName("kumulus-PlanningViewStudent");
        studentPanel.add(new Label(planningConstants.student()));
        roleTagsPanel.add(studentPanel);
        menuPanel.add(roleTagsPanel);        
        
        // Initialize this widget and set the CSS style
        initWidget(menuPanel);
        setStyleName("kumulus-PlanningMenu");
	}
    
	/**
	 * Sets the text on the label widget to the given string.
	 * 
	 * @param dateText The string to show on the label widget
	 */
    public void setDate(String dateText) {
        this.dateViewText.setText(dateText);
    }
    
    /**
     * Handler function for all click events of the menu widget.
     * 
     * Takes care of the period size selection and the setting of the current
     * period, either via the previous/next buttons or the today button.
     * 
     * @param clickedWidget A reference to the widget object that triggered this handler
     */
    public void onClick(Widget clickedWidget) {
        if (clickedWidget == this.weekLink) {
            this.calendar.setSelectionMode(Calendar.WEEKLY);
        } else if(clickedWidget == this.next4DaysLink) {
            this.calendar.setSelectionMode(Calendar.NEXT4DAYS);
        } else if (clickedWidget == this.monthLink) {
            this.calendar.setSelectionMode(Calendar.MONTHLY);
        } else if (clickedWidget == this.nextLink) {
            this.calendar.moveToNext();
        } else if (clickedWidget == this.previousLink) {
            this.calendar.moveToPrevious();
        } else if (clickedWidget == this.todayButton) {
            this.calendar.setDatesFromDate(new Date());
        } else if (clickedWidget == this.frLink) {
        	Window.open("Kumulus.html?locale=" + "fr", "_self", "");
        } else if (clickedWidget == this.gbLink) {
        	Window.open("Kumulus.html?locale=" + "en", "_self", "");
        } else if (clickedWidget == this.ptLink) {
        	Window.open("Kumulus.html?locale=" + "pt", "_self", "");
        }
    }

    /**
     * Handler function for the date update event of the menu widget.
     * 
     * Updates the date on all different widgets of the menu.
     * 
     * @param calendarWidget A reference to the Calendar widget that triggered this handler
     */
	public void onDateUpdate(Calendar calendarWidget) {
        Date[] datesSelected = calendarWidget.getDatesSelected();
        String dateStr = Calendar.dayShortMonthYeartFormat.format(datesSelected[0]);
        if (datesSelected.length > 1) {
            dateStr += " - " + Calendar.dayShortMonthYeartFormat.format(datesSelected[datesSelected.length-1]);
        }
        this.dateViewText.setText(dateStr);
    }

}
